

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cortex.built_ins.models.adversarial_autoencoder &mdash; Cortex2.0  documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 

  
  <script src="../../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../../index.html" class="icon icon-home"> Cortex2.0
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">User Documentation</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../install.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../getting_started.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules.html">cortex</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../develop.html">Develop</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../build.html">Custom demos</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../build.html#a-walkthrough-a-custom-classifier">A walkthrough a custom classifier:</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../build.html#defining-losses-and-results">Defining losses and results</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../build.html#visualization">Visualization</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../build.html#putting-it-together">Putting it together</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Cortex2.0</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>cortex.built_ins.models.adversarial_autoencoder</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cortex.built_ins.models.adversarial_autoencoder</h1><div class="highlight"><pre>
<span></span><span class="sd">&#39;&#39;&#39;Adversarial autoencoder</span>

<span class="sd">&#39;&#39;&#39;</span>

<span class="kn">from</span> <span class="nn">cortex.plugins</span> <span class="k">import</span> <span class="n">ModelPlugin</span><span class="p">,</span> <span class="n">register_model</span>
<span class="kn">from</span> <span class="nn">cortex.built_ins.models.gan</span> <span class="k">import</span> <span class="p">(</span><span class="n">SimpleDiscriminator</span><span class="p">,</span> <span class="n">GradientPenalty</span><span class="p">,</span>
                                         <span class="n">generator_loss</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">cortex.built_ins.models.vae</span> <span class="k">import</span> <span class="n">ImageDecoder</span><span class="p">,</span> <span class="n">ImageEncoder</span>


<div class="viewcode-block" id="AdversarialAutoencoder"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.adversarial_autoencoder.AdversarialAutoencoder">[docs]</a><span class="k">class</span> <span class="nc">AdversarialAutoencoder</span><span class="p">(</span><span class="n">ModelPlugin</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;Adversarial Autoencoder</span>

<span class="sd">    Autoencoder with a GAN loss on the latent space.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="n">defaults</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
        <span class="n">data</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">batch_size</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">train</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span> <span class="n">test</span><span class="o">=</span><span class="mi">640</span><span class="p">),</span>
                  <span class="n">inputs</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">inputs</span><span class="o">=</span><span class="s1">&#39;images&#39;</span><span class="p">)),</span>
        <span class="n">optimizer</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">optimizer</span><span class="o">=</span><span class="s1">&#39;Adam&#39;</span><span class="p">,</span> <span class="n">learning_rate</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">),</span>
        <span class="n">train</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">epochs</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span> <span class="n">archive_every</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
    <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="n">encoder_contract</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">kwargs</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">dim_out</span><span class="o">=</span><span class="s1">&#39;dim_z&#39;</span><span class="p">))</span>
        <span class="n">decoder_contract</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">kwargs</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">dim_in</span><span class="o">=</span><span class="s1">&#39;dim_z&#39;</span><span class="p">))</span>
        <span class="n">disc_contract</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">kwargs</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">dim_in</span><span class="o">=</span><span class="s1">&#39;dim_z&#39;</span><span class="p">))</span>
        <span class="n">penalty_contract</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">nets</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">network</span><span class="o">=</span><span class="s1">&#39;discriminator&#39;</span><span class="p">))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">encoder</span> <span class="o">=</span> <span class="n">ImageEncoder</span><span class="p">(</span><span class="n">contract</span><span class="o">=</span><span class="n">encoder_contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">decoder</span> <span class="o">=</span> <span class="n">ImageDecoder</span><span class="p">(</span><span class="n">contract</span><span class="o">=</span><span class="n">decoder_contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span> <span class="o">=</span> <span class="n">SimpleDiscriminator</span><span class="p">(</span><span class="n">contract</span><span class="o">=</span><span class="n">disc_contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">penalty</span> <span class="o">=</span> <span class="n">GradientPenalty</span><span class="p">(</span><span class="n">contract</span><span class="o">=</span><span class="n">penalty_contract</span><span class="p">)</span>

<div class="viewcode-block" id="AdversarialAutoencoder.build"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.adversarial_autoencoder.AdversarialAutoencoder.build">[docs]</a>    <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">noise_type</span><span class="o">=</span><span class="s1">&#39;normal&#39;</span><span class="p">,</span> <span class="n">dim_z</span><span class="o">=</span><span class="mi">64</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>

<span class="sd">        Args:</span>
<span class="sd">            noise_type: Prior noise distribution.</span>
<span class="sd">            dim_z: Dimensionality of latent space.</span>

<span class="sd">        &#39;&#39;&#39;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">add_noise</span><span class="p">(</span><span class="s1">&#39;Z&#39;</span><span class="p">,</span> <span class="n">dist</span><span class="o">=</span><span class="n">noise_type</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">dim_z</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">encoder</span><span class="o">.</span><span class="n">build</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">decoder</span><span class="o">.</span><span class="n">build</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">build</span><span class="p">()</span></div>

<div class="viewcode-block" id="AdversarialAutoencoder.routine"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.adversarial_autoencoder.AdversarialAutoencoder.routine">[docs]</a>    <span class="k">def</span> <span class="nf">routine</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inputs</span><span class="p">,</span> <span class="n">Z</span><span class="p">,</span> <span class="n">encoder_loss_type</span><span class="o">=</span><span class="s1">&#39;non-saturating&#39;</span><span class="p">,</span>
                <span class="n">measure</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">beta</span><span class="o">=</span><span class="mf">1.0</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>

<span class="sd">        Args:</span>
<span class="sd">            encoder_loss_type: Adversarial loss type for the encoder.</span>
<span class="sd">            beta: Amount of adversarial loss for the encoder.</span>

<span class="sd">        &#39;&#39;&#39;</span>

        <span class="n">Z_Q</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encoder</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">inputs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">decoder</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="n">Z_Q</span><span class="p">)</span>

        <span class="n">E_pos</span><span class="p">,</span> <span class="n">E_neg</span><span class="p">,</span> <span class="n">P_samples</span><span class="p">,</span> <span class="n">Q_samples</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">score</span><span class="p">(</span>
            <span class="n">Z</span><span class="p">,</span> <span class="n">Z_Q</span><span class="p">,</span> <span class="n">measure</span><span class="p">)</span>

        <span class="n">adversarial_loss</span> <span class="o">=</span> <span class="n">generator_loss</span><span class="p">(</span>
            <span class="n">Q_samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">,</span> <span class="n">loss_type</span><span class="o">=</span><span class="n">encoder_loss_type</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">losses</span><span class="o">.</span><span class="n">encoder</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">losses</span><span class="o">.</span><span class="n">decoder</span> <span class="o">+</span> <span class="n">beta</span> <span class="o">*</span> <span class="n">adversarial_loss</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="o">.</span><span class="n">adversarial_loss</span> <span class="o">=</span> <span class="n">adversarial_loss</span><span class="o">.</span><span class="n">item</span><span class="p">()</span></div>

<div class="viewcode-block" id="AdversarialAutoencoder.train_step"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.adversarial_autoencoder.AdversarialAutoencoder.train_step">[docs]</a>    <span class="k">def</span> <span class="nf">train_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n_discriminator_updates</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>

<span class="sd">        Args:</span>
<span class="sd">            n_discriminator_updates: Number of discriminator updates per step.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_discriminator_updates</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
            <span class="n">inputs</span><span class="p">,</span> <span class="n">Z</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">inputs</span><span class="p">(</span><span class="s1">&#39;inputs&#39;</span><span class="p">,</span> <span class="s1">&#39;Z&#39;</span><span class="p">)</span>
            <span class="n">Z_Q</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encoder</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">inputs</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">Z</span><span class="p">,</span> <span class="n">Z_Q</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">optimizer_step</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">penalty</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">optimizer_step</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">auto_input</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">optimizer_step</span><span class="p">()</span></div>

<div class="viewcode-block" id="AdversarialAutoencoder.eval_step"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.adversarial_autoencoder.AdversarialAutoencoder.eval_step">[docs]</a>    <span class="k">def</span> <span class="nf">eval_step</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
        <span class="n">inputs</span><span class="p">,</span> <span class="n">Z</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">inputs</span><span class="p">(</span><span class="s1">&#39;inputs&#39;</span><span class="p">,</span> <span class="s1">&#39;Z&#39;</span><span class="p">)</span>
        <span class="n">Z_Q</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encoder</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">inputs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">Z</span><span class="p">,</span> <span class="n">Z_Q</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">penalty</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">auto_input</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>

<div class="viewcode-block" id="AdversarialAutoencoder.visualize"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.adversarial_autoencoder.AdversarialAutoencoder.visualize">[docs]</a>    <span class="k">def</span> <span class="nf">visualize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inputs</span><span class="p">,</span> <span class="n">Z</span><span class="p">,</span> <span class="n">targets</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">decoder</span><span class="o">.</span><span class="n">visualize</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">encoder</span><span class="o">.</span><span class="n">visualize</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="n">targets</span><span class="p">)</span>

        <span class="n">Z_Q</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encoder</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">inputs</span><span class="p">)</span>
        <span class="n">R</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">decoder</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">Z_Q</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_image</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;ground truth&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_image</span><span class="p">(</span><span class="n">R</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;reconstructed&#39;</span><span class="p">)</span></div></div>


<span class="n">register_model</span><span class="p">(</span><span class="n">AdversarialAutoencoder</span><span class="p">)</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, MILA.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../../',
            VERSION:'',
            LANGUAGE:'None',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>